#pragma once
#include "..\common\GameDefine.h"

class RedDogMpServerLogical
    : public IGameLogical,
    public IPlayerIOEvent
{
protected:
    IUserList*      m_pUserList;
	RedDogUserList  m_UserList;
    IServerBase*    m_pServerBase;
    GUID            m_Guid;

public:
    STDMETHOD_(HRESULT, QueryInterface)(REFIID riid, void** ppvObject);
	STDMETHOD_(ULONG, AddRef)(){return 1;}
	STDMETHOD_(ULONG, Release)(){
		delete this;
		return 0;
	}

private:
    DWORD           m_dwRef;

public:
	STDMETHOD(GetEstimateTime)(DWORD* pMilleseSecond){
		*pMilleseSecond = 3000;
		return S_OK;
	}
	STDMETHOD(GetPlayerNum)(IGameConst::PLAYERNUMBER* pNumber){
		pNumber->nMaintenance = 1;
		pNumber->nMax         = 1;
		pNumber->nStart       = 0;
		return S_OK;
	}
	STDMETHOD(GetGameIdentifier)(char* lpszIdentifier){
		strcpy(lpszIdentifier, APPLICATION_NAME);
		return S_OK;
	}
	STDMETHOD(GetVersion)(VERSION_STRUCT* pVS){
		pVS->curVersion = MAKELONG(1, 1);
		pVS->maxVersion = MAKELONG(1, 1);
		pVS->minVersion = MAKELONG(1, 1);
		return S_OK;
	}

    //////////////////////////////////////////////////////////////////////////
    // IGameLogical
    IServerSite*    m_pSite;
    STDMETHOD(SetServerSite)(IServerSite* pSite);
	STDMETHOD(ConstructScene)(){return S_OK;}
	STDMETHOD(GetPersonalScene)(int nIdx, UINT uflags, SCENE* pScene){return S_OK;}
    STDMETHOD(Start)(){return S_OK;}
	STDMETHOD(Stop)(UINT uflags){
		if(uflags == STOP_SERVERERROR) {
			m_UserList.clear();
		}
		return S_OK;
	}
    STDMETHOD(Clear)(){return S_OK;}
	STDMETHOD(GetScores)(LPUSERSCORE pUserScore){
		memset(pUserScore, 0, sizeof(USERSCORE));
		return S_OK;
	}
    STDMETHOD(OnGameOperation)(int nIdx, DWORD dwUserID, int opID, LPOPDATA oData, int nSize){return S_OK;}
    STDMETHOD(OnRefreshScore)(LPUSERSCORE pUserScore){return S_OK;}

    //////////////////////////////////////////////////////////////////////////
    // IPlayerIOEvent
    STDMETHOD(OnUserEnter)(int nIdx, IGamePlayer* pPlayer);
    STDMETHOD(OnUserExiting)(int nIdx, IGamePlayer* pPlayer){return S_OK;}
    STDMETHOD(OnUserExit)(int nIdx, IGamePlayer* pPlayer);
    STDMETHOD(OnUserOffline)(int nIdx, IGamePlayer* pPlayer){return S_OK;}
    STDMETHOD(OnUserReplay)(int nIdx, IGamePlayer* pPlayer){return S_OK;}
public:
	UserNode* FindPlayer(IGamePlayer* pPlayer){
		ASSERT(pPlayer);

		RedDogUserList::iterator i = m_UserList.begin();
		for( ; i != m_UserList.end(); ++ i) {
			if(i->dwUserID == pPlayer->get_ID())
				break;
		}
		return i != m_UserList.end() ? &(*i) : 0;
	}
	UserNode* FindPlayer(DWORD dwUserID){
		RedDogUserList::iterator i = m_UserList.begin();
		for( ; i != m_UserList.end(); ++ i) {
			if(i->dwUserID == dwUserID)
				break;
		}
		return i != m_UserList.end() ? &(*i) : 0;
	}
    IServerBase* GetServerBase() const { return m_pServerBase; }
    IServerSite* GetServerSite() const { return m_pSite; }
};

